package cn.rongdao.jrkabao.ui.takephoto.uicomponent;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.AttributeSet;
import android.view.View;

import com.mifengkong.frtools.http.request.callback.BaseImpl;
import com.mifengkong.frtools.http.request.callback.FRBaseObserver;
import com.mifengkong.frtools.util.FRLog;
import com.mifengkong.frwidget.PercentLinearLayout;

import cn.rongdao.jrkabao.R;
import cn.rongdao.jrkabao.base.FRBaseTakePhotoActivity;
import cn.rongdao.jrkabao.bean.takephoto.IdentifySwitchBean;
import cn.rongdao.jrkabao.model.cashadvance.InsertUserModel;
import cn.rongdao.jrkabao.model.takephoto.IdentifySwitchModel;
import cn.rongdao.jrkabao.util.CommonUtil;
import cn.rongdao.jrkabao.util.growingio.BuriedUtil;

/**
 * Created by jack on 2017/10/10
 */

public class IdentifyUIComponent extends PercentLinearLayout {

    public IdentifyUIComponent(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    private ThreeTakePhotoComponent mThreeTakePhotoComponent;

    private OnSelectIdentifyPicClickListener onSelectIdentifyPicClickListener;

    private int flag;

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        mThreeTakePhotoComponent = findViewById(R.id.ltipc_take_photo);

        initListener();
    }

    private void setContent(IdentifySwitchBean data) {
        if (!CommonUtil.isStatusSuccess(data.getFront_img().getIs_show())) {
            mThreeTakePhotoComponent.setPositivePiucVisiable(View.GONE);
        } else {
            mThreeTakePhotoComponent.setPositivePiucVisiable(View.VISIBLE);
            flag++;
        }
        if (!CommonUtil.isStatusSuccess(data.getBack_img().getIs_show())) {
            mThreeTakePhotoComponent.setNegativePiucVisiable(View.GONE);
        } else {
            mThreeTakePhotoComponent.setNegativePiucVisiable(View.VISIBLE);
            flag++;
        }
        if (!CommonUtil.isStatusSuccess(data.getHand_img().getIs_show())) {
            mThreeTakePhotoComponent.setHandheldPiucVisiable(View.GONE);
        } else {
            mThreeTakePhotoComponent.setHandheldPiucVisiable(View.VISIBLE);
            flag++;
        }
        mThreeTakePhotoComponent.setVisibility(View.VISIBLE);
    }

    private void initListener() {
        mThreeTakePhotoComponent.setImageViewOnclickListener(new ThreeTakePhotoComponent.ImageViewOnclickListener() {
            @Override
            public void onPositivePicClickListener(View view) {
                if (null != onSelectIdentifyPicClickListener) {
                    BuriedUtil.kamao_shenfenyanzheng_anniu_apply(getContext(), 0);
                    onSelectIdentifyPicClickListener.onSelectPicClick(FRBaseTakePhotoActivity.PIC_WITHOUT_PERSON_FRONT, flag);
                }
            }

            @Override
            public void onNegativePicClickListener(View view) {
                if (null != onSelectIdentifyPicClickListener) {
                    BuriedUtil.kamao_shenfenyanzheng_anniu_apply(getContext(), 1);
                    onSelectIdentifyPicClickListener.onSelectPicClick(FRBaseTakePhotoActivity.PIC_WITHOUT_PERSON_OPPOSITE, flag);
                }
            }

            @Override
            public void onHandheldPicClickListener(View view) {
                if (null != onSelectIdentifyPicClickListener) {
                    BuriedUtil.kamao_shenfenyanzheng_anniu_apply(getContext(), 2);
                    onSelectIdentifyPicClickListener.onSelectPicClick(FRBaseTakePhotoActivity.PIC_WITH_PERSON, flag);
                }
            }
        });
    }

    public void setPositivePic(Bitmap bm) {
        mThreeTakePhotoComponent.setPositivePic(bm);
    }

    public void setNegativePic(Bitmap bm) {
        mThreeTakePhotoComponent.setNegativePic(bm);
    }

    public void setHandheldPic(String imageViewPath) {
        mThreeTakePhotoComponent.setHandheldPic(imageViewPath);
    }

    public void setOnSelectIdentifyPicClickListener(OnSelectIdentifyPicClickListener onSelectIdentifyPicClickListener) {
        this.onSelectIdentifyPicClickListener = onSelectIdentifyPicClickListener;
    }

    private void insertUser(BaseImpl baseImpl) {
        flag = 0;
        InsertUserModel.getInstance().execute(new FRBaseObserver<String>(baseImpl) {
            @Override
            protected void onBaseNext(String data) {

            }
        });
        IdentifySwitchModel.getInstance().execute(new FRBaseObserver<IdentifySwitchBean>(baseImpl, "加载中，请稍候...") {
            @Override
            protected void onBaseNext(IdentifySwitchBean data) {
                setContent(data);
            }
        });
    }

    public void initData(BaseImpl baseImpl) {
        insertUser(baseImpl);
    }

    public interface OnSelectIdentifyPicClickListener {
        void onSelectPicClick(int type, int pictureSize);
    }
}